/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> respath;
    vector<int> path;
    int sum = 0;
    int target;
    void dfs(TreeNode *cur){
        if (cur==nullptr) return;
      
            path.push_back(cur->val);//先当前值，然后左值
        sum+=cur->val;
      
        if (cur->left==nullptr&&cur->right==nullptr){
            if (sum==target){
                respath.push_back(path);
            }
            return;
        }
        if (cur->left!=nullptr)
       {
            dfs(cur->left);
        sum-=cur->left->val;//遍历完了左
        path.pop_back();
       }

        if(cur->right!=nullptr){
            dfs(cur->right);
                sum-=cur->right->val;
                path.pop_back();
        }
        
    }
    vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
        target = targetSum;
        dfs(root);
        return respath;
    }
};